package com.lzw.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzw.bean.User;
import com.lzw.common.bean.Result;
import com.lzw.common.mapper.BaseMapper;
import com.lzw.common.param.PageParam;
import com.lzw.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;

/**
 * 基础服务实现类，提供默认CRUD操作
 * Created by 林志伟 on 2017/5/11.
 */
public class BaseServiceImpl<  M extends BaseMapper,T> extends CommonService implements BaseService<T> {
    @Value(value = "${huiadmin.page.pageSize}")
    private int pageSize;

    /**
     * 基础mapper
     */
    @Autowired
    public M baseMapper;

    @Override
    public Result insert(T o) {
        if(baseMapper.insert(o)>=1){
            return  this.success("增加成功",o);
        }else{
            return  this.error("增加失败",o);
        }
    }

    @Override
    public Result update(T o) {
        if(baseMapper.update(o)>=1){
            return  this.success("修改成功",o);
        }else{
            return  this.error("修改失败",o);
        }
    }

    @Override
    public Result delete(Integer id) {
        if(baseMapper.delete(id)>=1){
            return  this.success("删除成功");
        }else{
            return  this.error("删除失败");
        }
    }

    @Override
    public Result deleteAll(int[] ids) {
        int num=baseMapper.deleteAll(ids);
        if(num<=0){
            return   this.error("删除失败");
        }else{
            return  this.success("成功删除"+num+"条记录");
        }
    }

    @Override
    public T find(Integer id) {
        return (T) baseMapper.find(id);
    }

    @Override
    public List<T> select( int size) {
        List<T>  users=baseMapper.select(size);
        return users;
    }

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @Override
    public PageInfo<T> page(PageParam pageParam) {
        if(pageParam.getPage()<=0){
            pageParam.setPage(1);
        }
        if(pageSize<=0){
            pageSize=10;
        }
        PageHelper.startPage(pageParam.getPage(),pageSize);
        List<T> lists= baseMapper.page(pageParam);
        PageInfo<T> pageInfo=new PageInfo<T>(lists);
        return pageInfo;
    }

    @Override
    public Result open(int id) {

        int num=baseMapper.open(id);
        if(num>=1){
            return  this.success("启用成功");
        }else {
            return  this.error("启用失败");
        }
    }

    @Override
    public Result close(int id) {
        int num=baseMapper.close(id);
        if(num>=1){
            return  this.success("" +
                    "禁用成功");
        }else {
            return  this.error("禁用失败");
        }
    }


}
